package com.wswy.wzcx.ui.car;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.TextView;
import com.blankj.utilcode.util.SizeUtils;
import com.google.android.flexbox.AlignContent;
import com.google.android.flexbox.FlexboxLayout;
import com.google.android.flexbox.FlexboxLayout.LayoutParams;
import com.google.android.flexbox.JustifyContent;
import com.wswy.wzcx.R;

public class LicensePlateDialog extends BottomSheetDialog{

  private static final String TAG = "LicensePlateDialog";

  private static final String[] provinces = {"京", "津", "沪", "渝", "冀", "晋", "辽", "吉",
      "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫",
      "鄂", "湘", "粤", "甘", "川", "贵", "琼", "云",
      "青", "陕", "桂", "藏", "宁", "新", "蒙"};

  private static final char[] alphabet = {'Q','W','E','R','T','Y','U','I','O','P',
      'A','S','D','F','G','H','J','K','L',
      'Z','X','C','V','B','N','M'};



  private FlexboxLayout flexboxLayout;


  private OnSelectedPrefixCallback onSelectedPrefixCallback;

  private String prefixProvince;
  private String prefixAlphabet;

  private int maxWidth;

  public LicensePlateDialog(@NonNull Context context) {
    super(context);

    setContentView(R.layout.dialog_choose_plate);

    flexboxLayout = findViewById(R.id.flexbox_layout);

    View.OnClickListener listener = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        onClickBtns(v);
      }
    };

    findViewById(R.id.btn_cancel).setOnClickListener(listener);
    findViewById(R.id.btn_ok).setOnClickListener(listener);


    flexboxLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        maxWidth = flexboxLayout.getMeasuredWidth();
        showProvinces();
        flexboxLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
      }
    });


  }


  public LicensePlateDialog setOnSelectedPrefixCallback(
      OnSelectedPrefixCallback onSelectedPrefixCallback) {
    this.onSelectedPrefixCallback = onSelectedPrefixCallback;
    return this;
  }

  private void showProvinces(){
    Context context = getContext();

    flexboxLayout.removeAllViewsInLayout();

    flexboxLayout.setJustifyContent(JustifyContent.FLEX_START);
    flexboxLayout.setAlignContent(AlignContent.FLEX_START);

    int margin = SizeUtils.dp2px(2);
    int columns = 8;

    int h = (maxWidth-(columns)*margin*2)/columns;

    View.OnClickListener provincesClickListener = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        prefixProvince = String.valueOf(((TextView) v).getText());
        showAlphabet();
      }
    };




    for (int i = 0; i < provinces.length; i++) {


      TextView textView = new TextView(context);
      textView.setTextColor(ContextCompat.getColorStateList(context, R.color.color_select_plate));
      textView.setBackgroundResource(R.drawable.plate_province_btn_background_r3);
      textView.setClickable(true);
      textView.setTextSize(20);
      textView.setGravity(Gravity.CENTER);
      textView.setText(provinces[i]);

      FlexboxLayout.LayoutParams lp = new LayoutParams(h, h);
      lp.leftMargin = lp.topMargin = lp.rightMargin = lp.bottomMargin = margin;

      if (i / columns > 0 && i % columns == 0) {
        lp.setWrapBefore(true);
      }

      textView.setOnClickListener(provincesClickListener);
      flexboxLayout.addView(textView, lp);
    }

  }


  private void showAlphabet(){
    Context context = getContext();

    flexboxLayout.removeAllViewsInLayout();

    flexboxLayout.setJustifyContent(JustifyContent.CENTER);
    flexboxLayout.setAlignContent(AlignContent.CENTER);


    int marginL = SizeUtils.dp2px(3);

    int w = (maxWidth-10*marginL*2)/10;
    int h = (int) (w * 1.33333f);

    int marginT = SizeUtils.dp2px(6);

    final View[] lastSelected = new View[1];

    View.OnClickListener alphabetClickListener = new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if(lastSelected[0] != null){
          lastSelected[0].setSelected(false);
        }

        prefixAlphabet=(String.valueOf(((TextView) v).getText()));

        v.setSelected(true);
        lastSelected[0]  =v;
        v.postDelayed(new Runnable() {
          @Override
          public void run() {
            findViewById(R.id.btn_ok).performClick();
          }
        },100);

      }
    };

    for (char s : alphabet) {
      TextView textView = new TextView(context);
      textView.setTextColor(ContextCompat.getColorStateList(context, R.color.color_select_plate));
      textView.setBackgroundResource(R.drawable.plate_alphabet_btn_background_r3);
      textView.setClickable(true);
      textView.setTextSize(20);
      textView.setGravity(Gravity.CENTER);
      textView.setText(String.valueOf(s));
      textView.setOnClickListener(alphabetClickListener);

      FlexboxLayout.LayoutParams lp = new LayoutParams(w, h);
      lp.leftMargin =  lp.rightMargin =  marginL;

      lp.topMargin = lp.bottomMargin =marginT;

      if (s == 'A' || s == 'Z') {
        lp.setWrapBefore(true);
      }

      flexboxLayout.addView(textView, lp);
    }

  }




  private void onClickBtns(View view){
    int id = view.getId();
    if(id == R.id.btn_cancel){

    }else if(id == R.id.btn_ok){
      if(onSelectedPrefixCallback != null && !TextUtils.isEmpty(prefixProvince) && !TextUtils.isEmpty(prefixAlphabet)){
        onSelectedPrefixCallback.onSelected(prefixProvince,prefixAlphabet);
      }
    }
    dismiss();
  }


  public interface OnSelectedPrefixCallback{
    void onSelected(String province,String alphabet);
  }

}
